/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2021-09-22
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "pwm.h"
#include "hal_motor.h"
#include "bsp_hw_config.h"


#define HAL_MOTOR_LOG_EN		0

#if HAL_MOTOR_LOG_EN
#include "log.h"
#define HAL_MOTOR_LOG			LOG
#else
#define HAL_MOTOR_LOG(...)
#endif

/*----------------------------macro file---------------------------*/
#define PWM_MAX_VALUE		100
#define PWM_PER_VALUE		(60*(PWM_MAX_VALUE/100))

#define MOTOR_SHAKE_MS		1000

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

void hal_motor_start(void)
{
#if (MOTOR_USE_PWM == 1)
    hal_pwm_init(BSP_MOTOR_PWM_CH_NUM, PWM_CLK_DIV_32, PWM_CNT_UP, PWM_POLARITY_FALLING);
    hal_pwm_set_count_val(BSP_MOTOR_PWM_CH_NUM, PWM_PER_VALUE, PWM_MAX_VALUE);
    hal_pwm_open_channel(BSP_MOTOR_PWM_CH_NUM, BSP_MOTOR_PIN_NUM);
    hal_pwm_start();
#else
    hal_gpio_pin_init(BSP_MOTOR_PIN_NUM, GPIO_OUTPUT);
    hal_gpio_write(BSP_MOTOR_PIN_NUM, 1);
#endif
}

void hal_motor_stop(void)
{
#if (MOTOR_USE_PWM == 1)
    hal_pwm_close_channel(BSP_MOTOR_PWM_CH_NUM);
    hal_pwm_destroy(BSP_MOTOR_PWM_CH_NUM);
    hal_pwm_stop();
#endif
    hal_gpio_pull_set(BSP_MOTOR_PIN_NUM, FLOATING);
    hal_gpio_write(BSP_MOTOR_PIN_NUM, 0);
}
